This got me thinking about how I structure things, which generally results from starting with a small one-file prototype and then breaking bits of it off into different files. Here's an example (all these can be run with qmlscene main.qml):
First you might have
File main.qml
// main.qml
import QtQuick 2.7
Rectangle {
id: main
width: 640
height: 480
property string msg0: "Some text"
property string msg1: "Some more text"
Column {
anchors.centerIn: parent
Text {text: main.msg0}
Text {text: main.msg1}
}
}
then you might try and organize stuff a bit:
import QtQuick 2.7
Rectangle {
id: main
width: 640
height: 480
Item {
id: config
property string msg0: "Some text"
property string msg1: "Some more text"
}
Column {
anchors.centerIn: parent
Text {text: config.msg0}
Text {text: config.msg1}
}
}
then you might split that up with a Config.qml:
import QtQuick 2.7
Item {
property string msg0: "Some text"
property string msg1: "Some more text"
}
and main.qml now simplified to:
import QtQuick 2.7
Rectangle {
id: main
width: 640
height: 480
Config {id: config}
Column {
anchors.centerIn: parent
Text {text: config.msg0}
Text {text: config.msg1}
}
}
and then you start moving out other bits of functionality e.g adding a Messages.qml:
import QtQuick 2.7
Column {
anchors.centerIn: parent
Text {text: config.msg0}
Text {text: config.msg1}
}
and main.qml now simplified to:
import QtQuick 2.7
Rectangle {
id: main
width: 640
height: 480
Config {id: config}
Messages {}
}
I've never felt any need for singletons in QML code at all. (Having the hosting C++ set some context properties based on environment or command-line-options is the probably the closest I've come).